package server.classloder;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MyClassLoader extends ClassLoader {
    private String path;

    public MyClassLoader(String path) {
        this.path = path;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String myPath = path+"/"+name.replaceAll("\\.","/")+".class";
        System.out.println(myPath);
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;

        try {
            inputStream = new FileInputStream(myPath);
            outputStream = new ByteArrayOutputStream();
            int temp = 0;
            while ((temp = inputStream.read()) != -1) {
                outputStream.write(temp);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        byte[] bytes = outputStream.toByteArray();
        Class clazz = defineClass(name, bytes, 0, bytes.length);
        return clazz;
    }
}